package com.baichuan.zntx.service.impl;

import com.baichuan.common.core.utils.MapstructUtils;
import com.baichuan.common.core.utils.StringUtils;
import com.baichuan.common.mybatis.core.page.TableDataInfo;
import com.baichuan.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.baichuan.zntx.domain.bo.CalibrateBo;
import com.baichuan.zntx.domain.vo.CalibrateVo;
import com.baichuan.zntx.domain.Calibrate;
import com.baichuan.zntx.mapper.CalibrateMapper;
import com.baichuan.zntx.service.ICalibrateService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 标定点管理Service业务层处理
 *
 * @author MaZhanliang
 * @date 2024-05-24
 */
@RequiredArgsConstructor
@Service
public class CalibrateServiceImpl implements ICalibrateService {

    private final CalibrateMapper baseMapper;

    /**
     * 查询标定点管理
     */
    @Override
    public CalibrateVo queryById(String id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询标定点管理列表
     */
    @Override
    public TableDataInfo<CalibrateVo> queryPageList(CalibrateBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<Calibrate> lqw = buildQueryWrapper(bo);
        Page<CalibrateVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询标定点管理列表
     */
    @Override
    public List<CalibrateVo> queryList(CalibrateBo bo) {
        LambdaQueryWrapper<Calibrate> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<Calibrate> buildQueryWrapper(CalibrateBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<Calibrate> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getTitle()), Calibrate::getTitle, bo.getTitle());
        lqw.eq(bo.getLongtitude() != null, Calibrate::getLongtitude, bo.getLongtitude());
        lqw.eq(bo.getLatitude() != null, Calibrate::getLatitude, bo.getLatitude());
        lqw.eq(bo.getX() != null, Calibrate::getX, bo.getX());
        lqw.eq(bo.getY() != null, Calibrate::getY, bo.getY());
        lqw.orderByAsc(Calibrate::getOrder);
        return lqw;
    }

    /**
     * 新增标定点管理
     */
    @Override
    public Boolean insertByBo(CalibrateBo bo) {
        Calibrate add = MapstructUtils.convert(bo, Calibrate.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改标定点管理
     */
    @Override
    public Boolean updateByBo(CalibrateBo bo) {
        Calibrate update = MapstructUtils.convert(bo, Calibrate.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(Calibrate entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除标定点管理
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
